<template>
  <div style="width: 100%;overflow-y:scroll;height: calc(100vh - 72px)">
    <div class="modern-forms" style="margin-top: 120px">
      <div class="modern-container">
        <form style="text-align: left">
          <fieldset>
            <div class="mdn-group">
              <label class="field-group mdn-upload">
                <input v-model="imgpath" type="text" class="mdn-input" placeholder="no file selected" @click="selectImg" readonly>
                <label class="mdn-label">File Load</label>
                <span class="mdn-bar"></span>
                <span class="mdn-button btn-primary"> Choose Image </span>
              </label>
            </div>
            <template v-if="imgpath">
              <div class="form-row">
                <div class="mdn-group">
                  <label class="field-group mdn-upload">
                    <label class="mdn-label">Origin Image</label>
                    <img id="originImg" style="margin-top: 10px"/>
                  </label>
                </div>
                <br/>
                <div class="mdn-group">
                  <label class="field-group mdn-upload">
                    <label class="mdn-label">Space Domain</label>
                    <canvas id="grayImg" style="margin-top: 10px"/>
                  </label>
                </div>
                <div class="mdn-group">
                  <label class="field-group mdn-upload">
                    <label class="mdn-label">Frequency Domain</label>
                    <canvas id="frequencyImg" style="margin-top: 10px"/>
                  </label>
                </div>
              </div><!-- end form-row -->
              <div class="form-row">
                <div class="mdn-group">
                  <label class="field-group mdn-upload">
                    <label class="mdn-label">bandpass</label>
                    <canvas id="bandpassImg" style="margin-top: 10px"/>
                  </label>
                </div>
                <div class="mdn-group">
                  <label class="field-group mdn-upload">
                    <label class="mdn-label">bandreject</label>
                    <canvas id="bandrejectImg" style="margin-top: 10px"/>
                  </label>
                </div>
              </div><!-- end form-row -->
            </template>
          </fieldset>
        </form>
      </div><!-- modern-container -->
    </div><!-- modern-forms -->
  </div>
</template>

<script>
import * as d3 from 'd3'
import { sliderBottom } from 'd3-simple-slider';
var mutex = true
function Fourier(imgdata, u, v, M, N) {
  let Gray, conv = 0, iconv = 0;
  for(let x = 0;x < M;x++){
    for(let y = 0;y < N;y++){
      let index = y*M+x, t = 2*Math.PI*(u*x/M + v*y/N);
      let cos = Math.cos(t), isin = -Math.sin(t);
      Gray = imgdata[index*4]
      if((x+y)%2===1) Gray = - Gray;// 移动到中间
      conv += Gray * cos
      iconv += Gray * isin
    }
  }
  return [conv,iconv];
}
export default {
  name: "c4q4",
  data(){
    return{
      imgpath:null,
      processing: false
    }
  },
  methods:{
    selectImg(){
      let input = document.createElement('input');
      input.type = 'file';
      return new Promise(function (resolve) {
        input.onchange = function(ev) {
          resolve(ev.target.files[0])
          return false;
        };
        input.click();
      }).then(file=>{
        this.imgpath = file.path
        const that = this
        return new Promise(function (resolve) {
          var reader = new FileReader();
          reader.onload = function(e){
            // target.result 该属性表示目标对象的DataURL
            let img = document.getElementById('originImg')
            img.src = e.target.result
            img.onload = ()=>resolve(img)
          }
          // 传入一个参数对象即可得到基于该参数对象的文本内容
          reader.readAsDataURL(file);
        })
      }).then(img=>{
        let canvas = document.getElementById('grayImg')
        canvas.width = img.width * 0.8;
        canvas.height = img.height * 0.8;
        let ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, img.width * 0.4,img.height * 0.4);
        let imgData = ctx.getImageData(0,0,img.width * 0.8,img.height * 0.8)
        let len = img.width * img.height * 0.8 * 0.8;
        let R,G,B,Gray;
        for(let i = 0;i < len;i++){
          R = imgData.data[i*4]
          G = imgData.data[i*4+1]
          B = imgData.data[i*4+2]
          Gray = parseInt(R*0.299 + G*0.587 + B*0.114)// 彩色图转到频率域效果不好
          imgData.data[i*4] = imgData.data[i*4 + 1] = imgData.data[i*4 + 2] = Gray;
          imgData.data[i*4 + 3] = 255;
        }
        ctx.clearRect(0,0,img.width * 0.8,img.height * 0.8);
        ctx.putImageData(imgData, 0, 0);
        return Promise.resolve(canvas);
      }).then(img=>{
        let canvas = document.getElementById('frequencyImg')
        canvas.width = img.width;
        canvas.height = img.height;
        let ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, img.width,img.height);
        let imgData = ctx.getImageData(0,0,img.width,img.height)
        let M = img.width, N = img.height, len = img.width * img.height, store = [];
        for(let v = 0;v < N;v++){
          for(let u = 0;u < M;u++){
            let i = v * M + u;
            let [conv,iconv] = Fourier(imgData.data,u,v,M,N)
            if(u === 75) console.log(i,len,conv)
            store.push(conv,iconv);
          }
        }
        function nonlinearMaper(conv,iconv){
          return Math.round(20 * Math.log(Math.sqrt(conv*conv+iconv*iconv)));
        }
        for(let i = 0;i < len;i++){
          imgData.data[i*4] = imgData.data[i*4+1] = imgData.data[i*4+2] = nonlinearMaper(store[i*2],store[i*2+1]);
          imgData.data[i*4+3] = 255
        }
        ctx.clearRect(0,0,img.width,img.height);
        ctx.putImageData(imgData, 0, 0);
        return Promise.resolve([store,M,N])
      }).then(([frequency,M,N])=>{
        function doProcess(name,H){
          let len = frequency.length / 2;
          let canvas = document.getElementById(name)
          canvas.width = M;
          canvas.height = N;
          let ctx = canvas.getContext("2d");
          let imgData = ctx.getImageData(0,0,M,N)
          for(let v = 0;v < N;v++){
            for(let u = 0;u < M;u++){
              let Gray = 0;
              for(let x = 0;x < M;x++){
                for(let y = 0;y < N;y++){
                  let index = y*M+x, t = 2*Math.PI*(u*x/M + v*y/N);
                  let conv = frequency[index*2];
                  let iconv = frequency[index*2+1];
                  let cos = Math.cos(t), isin = Math.sin(t);
                  Gray += (conv*cos - iconv*isin) * H(x,y,M,N);
                }
              }
              let i = v * M + u;
              if(u === 75) console.log(i,len,Math.round(Gray / M / N))
              imgData.data[i*4] = imgData.data[i*4+1] = imgData.data[i*4+2] = Math.round(Math.abs(Gray) / M / N);
              imgData.data[i*4+3] = 255;
            }
          }
          ctx.clearRect(0,0,M,N);
          ctx.putImageData(imgData, 0, 0);
        }
        let D0 = 10,D1 = 15;
        doProcess('bandpassImg',(x,y,M,N)=>{
          let dx = (M/2-x), dy = (N/2-y);
          return dx*dx+dy*dy < D1*D1 && dx*dx+dy*dy > D0*D0 ? 1 : 0;
        })
        doProcess('bandrejectImg',(x,y,M,N)=>{
          let dx = (M/2-x), dy = (N/2-y);
          return dx*dx+dy*dy < D1*D1 && dx*dx+dy*dy > D0*D0 ? 0 : 1;
        })
      })
    }
  },
  mounted() {
  }
}
</script>

<style scoped>
.axis path,
.axis line{
  fill: none;
  stroke: black;
  shape-rendering: crispEdges;
}

.axis text {
  font-family: sans-serif;
  font-size: 11px;
}

.MyRect {
  fill: steelblue;
}

.MyText {
  fill: white;
  text-anchor: middle;
}

</style>